package com.icss.train_sys_back.controller;

import com.icss.train_sys_back.handler.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/home/websocket")
public class WebSocketTestController {

    @Autowired
    private MyWebSocketHandler webSocketHandler;

    @GetMapping("/test")
//    @SendTo("/")
    public String test() {
        return "WebSocket测试接口正常 - 当前时间: " + new java.util.Date();
    }

    @PostMapping("/sendToUser")
    public String sendToUser(@RequestParam String userId, @RequestParam String msg) {
        webSocketHandler.sendToUser(userId,msg);
        return "消息:  " + msg + "  已发送给" + userId;
    }

    @PostMapping("/broadcast")
    public String broadcastMessage(@RequestParam String message) {
        webSocketHandler.broadcast("系统广播: " + message + " - " + new java.util.Date());
        return "消息已广播给 " + MyWebSocketHandler.getConnectionCount() + " 个客户端";
    }

    @GetMapping("/connections")
    public String getConnectionCount() {
        return "当前WebSocket连接数: " + MyWebSocketHandler.getConnectionCount();
    }
}
